// Portlet Layers List
var mapWMS, mapWFS;

mapWMS = new Array(); // mang chua map service
mapWFS = new Array(); // mang chua feature service
var isParamBonita = "";
// Portlet Tools
var menuWS;
menuWS = new Array();

/****** Style Map *******/
// ElevationPoints
var colorSelectedPointElevation = "#FF0000"; // red
var colorUnSelectedPointElevation = "#336699"; // light blue
var colorSelectedPointDeleteElevation = "#FFFF00"; // yellow
var colorUnSelectedPointDeleteElevation = "#336699"; // light blue
// Committe
var colorSelectedPoint = "#FFA500"; // dark blue
var colorUnSelectedPoint = "#808080"; // gray
var colorSelectedPointDelete = "#FF0000"; // red
var colorUnSelectedPointDelete = "#808080"; // gray
// Roads
var colorSelectedLine = "#FF0000"; // red
var colorUnSelectedLine = "#00FF00"; // green
var colorSelectedLineDelete = "#0000CD"; // medium blue
var colorUnSelectedLineDelete = "#FF0000"; // green
// Commune
var colorSelectedFillPolygon = "#FFFF00"; // yellow
var colorUnSelectedFillPolygon = "#FFF8DC"; // Cornsilk
var colorSelectedFillPolygonDelete = "#00FFFF"; // aquq
var colorUnSelectedFillPolygonDelete = "#FF0000"; // red
var colorBorderPolygon = "#000000"; // black
var defStylePointElevation =
{
    strokeColor: colorUnSelectedPointElevation,
    strokeOpacity: "0.7",
    strokeWidth: 2,
    fillColor: colorUnSelectedPointElevation,
    pointRadius: 3,
    cursor: "pointer"
};
var defStylePoint =
{
    strokeColor: colorUnSelectedPoint,
    strokeOpacity: "0.7",
    strokeWidth: 2,
    fillColor: colorUnSelectedPoint,
    pointRadius: 5,
    cursor: "pointer"
};

var defStyleLine =
{
    strokeColor: colorUnSelectedLine,
    strokeOpacity: "0.7",
    strokeWidth: 2,
    fillColor: colorUnSelectedLine,
    pointRadius: 5,
    cursor: "pointer"
};

var defStylePolygon =
{
    strokeColor: colorBorderPolygon,
    strokeOpacity: "0.7",
    strokeWidth: 2,
    fillColor: colorUnSelectedFillPolygon,
    pointRadius: 5,
    cursor: "pointer"
};
// Measure
var defStyleMeasure = {
    "Point": {
        pointRadius: 4,
        graphicName: "square",
        fillColor: "white",
        fillOpacity: 1,
        strokeWidth: 1,
        strokeOpacity: 1,
        strokeColor: "#333333"
    },
    "Polyline": {
        strokeWidth: 3,
        strokeOpacity: 1,
        strokeColor: "#008B8B",
        strokeDashstyle: "dash"
    },
    "Polygon": {
        strokeWidth: 2,
        strokeOpacity: 1,
        strokeColor: "#666666",
        fillColor: "white",
        fillOpacity: 0.3
    }
};

var styPointElevation = OpenLayers.Util.applyDefaults(defStylePointElevation, OpenLayers.Feature.Vector.style["default"]);
var styPoint = OpenLayers.Util.applyDefaults(defStylePoint, OpenLayers.Feature.Vector.style["default"]);
var styLine = OpenLayers.Util.applyDefaults(defStyleLine, OpenLayers.Feature.Vector.style["default"]);
var styPolygon = OpenLayers.Util.applyDefaults(defStylePolygon, OpenLayers.Feature.Vector.style["default"]);
var styMeasure = new OpenLayers.Style();
styMeasure.addRules([new OpenLayers.Rule({symbolizer: defStyleMeasure})]);
var smPoint = new OpenLayers.StyleMap({
    'default': styPoint,
    'select':
    {
        strokeColor: colorSelectedPoint,
        fillColor: colorSelectedPoint
    },
    'delete_selection':
    {
        strokeColor: colorSelectedPointDelete,
        fillColor: colorSelectedPointDelete
    },
    'after_delete':
    {
        strokeOpacity: "0",
        display: "none"
    }
});

var smLine = new OpenLayers.StyleMap({
    'default': styLine,
    'select':
    {
        strokeColor: colorSelectedLine,
        fillColor: colorSelectedLine
    },
    'delete_selection':
    {
        strokeColor: colorSelectedLineDelete,
        fillColor: colorSelectedLineDelete
    },
    'after_delete':
    {
        strokeOpacity: "0",
        display: "none"
    }
});

var smPolygon = new OpenLayers.StyleMap({
    'default': styPolygon,
    'select':
    {
        strokeColor: colorBorderPolygon,
        fillColor: colorSelectedFillPolygon
    },
    'delete_selection':
    {
        strokeColor: colorSelectedFillPolygonDelete,
        fillColor: colorSelectedFillPolygonDelete
    },
    'after_delete':
    {
        strokeOpacity: "0",
        display: "none"
    }
});
var smMeasure = new OpenLayers.StyleMap({
	"default": styMeasure
});
/***** end style ******/

/*********************** INPUT DATA ***********************/
var titlePage = "";
var urlMapService = "";
var urlMapServer = "";
var urlFeatureServer = "";
var featureNSServer = "";
var featureNS = "";
var featureBase = "";
var projection = "";
var version = "";
var tolerance = 0;
var Node = false;
var Vertex = false;
var Edge = false;
var indexLayer = 1;
var layerSelected = null; // lop ban do duoc chon de thao tac
var isShowScaleBar = true;
var isShowOV = true;
var isToolExtension = true;

var sAppGis = "";
var lServerName = "";
var lImgPathOpenlayer = "";
var lProxyHost = "";
var bReadySnapXML = false;
var bReadyLanagueXML = false;
var bReadyLayerXML = false;
var bReadyMenuXML = false;
var bReadyResolutionsXML = false;

var arrayLayersInfo = [];

/********** language *****************
 * 
 */
var LANGUAGE = [];
LANGUAGE["none"] = '--';
var SCALELIST = [];
/***********************************/
/****** struct object *****/
var vectorParam = structData("AppGis|IndexLayer|LayerId|Layer_Name|Visible|StyleMap|Projection|SaveObj|Version|URL|Feature_Type|" +
		"Feature_Base|Feature_NS|Geometry_Name|Geometry_Type|DisplayClass_Draw|DisplayClass_Modify|DisplayClass_Del|" +
		"Map_Service|Base_Map|Display|IsGroupLayer|Module|Style|SearchAttr");

var menuParam = structData("ID|Name|Level|Group|Child|Child_ID|Height");

var snapSettings = structData("IndexLayer|Tolerance|Node|Vertex|Edge");

var XMin = 0, XMax = 0, YMin = 0, YMax = 0;
var XMinF = 0, XMaxF = 0, YMinF = 0, YMaxF = 0;

// doc thong tin file config
// menu: ID | Name | Level | Group | Child | Child_ID | Height
var menuItem = new Array();
$.get(sURL, function (data) {
    $(data).find('menus').each(function () {
        var $menus = $(this);
        $menus.find('menu').each(function () {
            var $menuData = $(this);
            var sID = $menuData.find('ID').text();
            var sName = $menuData.find('Name').text();
            var sLevel = $menuData.find('Level').text();
            var sGroup = '';
            if ($menuData.find('Group').text()) sGroup = $menuData.find('Group').text();
            var sChild = '';
            if ($menuData.find('Child').text()) sChild = $menuData.find('Child').text();
            var sChild_ID = '';
            if ($menuData.find('Child_ID').text()) sChild_ID = $menuData.find('Child_ID').text();
            var sHeight = '';
            if ($menuData.find('Height').text()) sHeight = $menuData.find('Height').text();
            var menuChild = new menuParam(sID, sName, sLevel, sGroup, sChild, sChild_ID, sHeight);
            menuItem[menuItem.length] = menuChild;

            // Portlet Tools
            if (sChild_ID == '' && sName != '-') {
                menuWS[menuWS.length] = menuChild;
            };
        });
        bReadyMenuXML = true;
    });

    $(data).find('server_Name').each(function () {
        var $server_Name = $(this);
        if ($server_Name.text()) lServerName = $server_Name.text();
    });

    $(data).find('img_path_openlayer').each(function () {
        var $img_path_openlayer = $(this);
        if ($img_path_openlayer.text()) lImgPathOpenlayer = $img_path_openlayer.text();
    });

    $(data).find('proxyhost').each(function () {
        var $proxyHost = $(this);
        if ($proxyHost.text()) lProxyHost = $proxyHost.text();
    });

    $(data).find('resolutions').each(function () {
    	var $resolutions = $(this);
    	var objChildNodes = $resolutions[0].childNodes;
    	for(var i=0; i<objChildNodes.length; i++)
    	{
    		var objItem = objChildNodes[i];
    		// comment
    		if(objItem.nodeType != 1) continue;    		
    		RESOLUTIONS[RESOLUTIONS.length] = $(objItem).text();
    	}
    	bReadyResolutionsXML = true;
    });

    $(data).find('scale_list').each(function () {
    	var $scaleList = $(this);
    	var objChildNodes = $scaleList[0].childNodes;
    	for(var i=0; i<objChildNodes.length; i++)
    	{
    		var objItem = objChildNodes[i];
    		// comment
    		if(objItem.nodeType != 1) continue;    		
    		SCALELIST[SCALELIST.length] = $(objItem).text();
    	}
    	bReadyScale = true;
    });

    $(data).find('Language').each(function () {
    	var $layerList = $(this);
    	var objChildNodes = $layerList[0].childNodes;
    	var i=0;
    	for(i=0; i<objChildNodes.length; i++)
    	{
    		var objItem = objChildNodes[i];
    		// comment
    		if(objItem.nodeType == 8) continue;
    		LANGUAGE[objItem.nodeName] = $(objItem).text();
    	}
    	bReadyLanagueXML = true;
    });
    
    $(data).find('bounds').each(function () {
        var $boundData = $(this);
        XMin = $boundData.find('xmin').text();
        XMax = $boundData.find('xmax').text();
        YMin = $boundData.find('ymin').text();
        YMax = $boundData.find('ymax').text();
    });
    
    $(data).find('fullExtent').each(function () {
        var $boundData = $(this);
        XMinF = $boundData.find('xmin').text();
        XMaxF = $boundData.find('xmax').text();
        YMinF = $boundData.find('ymin').text();
        YMaxF = $boundData.find('ymax').text();
    });

    $(data).find('titlePage').each(function () {
        var $titlePage = $(this);
        if ($titlePage.text()) titlePage = "<H2>" + $titlePage.text() + "</H2>";
    });

    $(data).find('URL_Map_Service').each(function () {
        var $urlMapService = $(this);
        if ($urlMapService.text()) urlMapService = $urlMapService.text();
    });
    
    $(data).find('URL_Map_Server').each(function () {
        var $urlServer = $(this);
        if ($urlServer.text()) urlMapServer = $urlServer.text();
    });

    $(data).find('URL_Feature_Server').each(function () {
        var $urlServer = $(this);
        if ($urlServer.text()) urlFeatureServer = $urlServer.text();
    });
    
    $(data).find('Feature_NS_Server').each(function () {
        var $featureNSServer = $(this);
        if ($featureNSServer.text()) featureNSServer = $featureNSServer.text();
    });
    
    $(data).find('Feature_NS').each(function () {
        var $featureNS = $(this);
        if ($featureNS.text()) featureNS = $featureNS.text();
    });

    $(data).find('Feature_Base').each(function () {
        var $featureBase = $(this);
        if ($featureBase.text()) featureBase = $featureBase.text();
    });

    $(data).find('Projection').each(function () {
        var $projection = $(this);
        if ($projection.text()) projection = $projection.text();
    });

    $(data).find('Version').each(function () {
        var $version = $(this);
        if ($version.text()) version = $version.text();
    });

    $(data).find('Scalbar').each(function () {
        var $Scalbar = $(this);
        if ($Scalbar.text()) isShowScaleBar = $Scalbar.text()=="0" ? false : true;
    });

    $(data).find('Overview').each(function () {
        var $overview = $(this);
        if ($overview.text()) isShowOV = $overview.text()=="0" ? false : true;
    });

    $(data).find('Tool_Extension').each(function () {
        var $toolExtension = $(this);
        if ($toolExtension.text()) isToolExtension = $toolExtension.text()=="0" ? false : true;
    });

    $(data).find('Layers').each(function () {
        var $layers = $(this);
        $layers.find('Layer').each(function () {
            var $layer = $(this);
            var appGis = '';
            if ($layer.find('AppGis').text()) appGis = $layer.find('AppGis').text();
            var layerId = '';
            if ($layer.find('LayerId').text()) layerId = $layer.find('LayerId').text();
            var layerName = '';
            if ($layer.find('Layer_Name').text()) layerName = $layer.find('Layer_Name').text();

            var visible = '';
            if ($layer.find('Visible').text()) visible = $layer.find('Visible').text();
            visible = visible == '1' ? true : false;

            var geometryName = '';
            if ($layer.find('Geometry_Name').text()) geometryName = $layer.find('Geometry_Name').text();

            var geometryType = '';
            if ($layer.find('Geometry_Type').text()) geometryType = $layer.find('Geometry_Type').text();

            var mapService = false;
            if ($layer.find('Map_Service').text()) mapService = $layer.find('Map_Service').text();
            mapService = mapService == '1' ? true : false;

            var baseMap = '';
            if ($layer.find('Base_Map').text()) baseMap = $layer.find('Base_Map').text();
            baseMap = baseMap == '1' ? true : false;

            var display = '';
            if ($layer.find('Display').text()) display = $layer.find('Display').text();
            display = display == '1' ? true : false;

            var isGroupLayer = '';
            if ($layer.find('IsGroupLayer').text()) isGroupLayer = $layer.find('IsGroupLayer').text();
            isGroupLayer = isGroupLayer == '1' ? true : false;

            var sModules = '';
            if ($layer.find('Modules').text()) sModules = $layer.find('Modules').text();

            var sStyleName = '';
            if ($layer.find('Style').text()) sStyleName = $layer.find('Style').text();
	            
            var sFeatureNS = featureNS;
            if ($layer.find('Feature_NS_Other').text()) sFeatureNS = $layer.find('Feature_NS_Other').text();
            
            var sFeatureBase = featureBase;
            if ($layer.find('Feature_Base_Other').text()) sFeatureBase = $layer.find('Feature_Base_Other').text();
            
            var urlWMS = urlMapServer;
            if ($layer.find('URL_Map_Server_Other').text()) urlWMS = $layer.find('URL_Map_Server_Other').text();

            var urlWFS = urlFeatureServer;
            
            var bSearchAttr = false;
            if ($layer.find('SearchAttr').text()) bSearchAttr = $layer.find('SearchAttr').text();
            bSearchAttr = bSearchAttr == '1' ? true : false;
            
            // StyleMap - SaveObj
            var styleMap = null;
            var saveObj = null;
            var classDraw = "olControlDrawFeaturePoint";
            var classModify = "olControlModifyFeature";
            var classDel = "olControlDeleteFeature";
            if (geometryType == Geometries.Point) {
            	var smPoint = new OpenLayers.StyleMap({
            	    'default': styPoint,
            	    'select':
            	    {
            	        strokeColor: colorSelectedPoint,
            	        fillColor: colorSelectedPoint
            	    },
            	    'delete_selection':
            	    {
            	        strokeColor: colorSelectedPointDelete,
            	        fillColor: colorSelectedPointDelete
            	    },
            	    'after_delete':
            	    {
            	        strokeOpacity: "0",
            	        display: "none"
            	    }
            	});
                styleMap = smPoint;
                if (sFeatureNS != '') {
                    saveObj = new OpenLayers.Strategy.Save();
                    saveObj.events.register('start', null, saveStartPoint);
                    saveObj.events.register('success', null, saveSuccessPoint);
                    saveObj.events.register('fail', null, saveFailPoint);
                }
            	classDraw = "olControlDrawFeaturePoint";
            }
            else if (geometryType == Geometries.Polyline) {
            	var smLine = new OpenLayers.StyleMap({
            	    'default': styLine,
            	    'select':
            	    {
            	        strokeColor: colorSelectedLine,
            	        fillColor: colorSelectedLine
            	    },
            	    'delete_selection':
            	    {
            	        strokeColor: colorSelectedLineDelete,
            	        fillColor: colorSelectedLineDelete
            	    },
            	    'after_delete':
            	    {
            	        strokeOpacity: "0",
            	        display: "none"
            	    }
            	});
                styleMap = smLine;
                if (sFeatureNS != '') {
                    saveObj = new OpenLayers.Strategy.Save();
                    saveObj.events.register('start', null, saveStartLine);
                    saveObj.events.register('success', null, saveSuccessLine);
                    saveObj.events.register('fail', null, saveFailLine);
                }
            	classDraw = "olControlDrawFeatureLine";
            }
            else if (geometryType == Geometries.Polygon) {
            	var smPolygon = new OpenLayers.StyleMap({
            	    'default': styPolygon,
            	    'select':
            	    {
            	        strokeColor: colorBorderPolygon,
            	        fillColor: colorSelectedFillPolygon
            	    },
            	    'delete_selection':
            	    {
            	        strokeColor: colorSelectedFillPolygonDelete,
            	        fillColor: colorSelectedFillPolygonDelete
            	    },
            	    'after_delete':
            	    {
            	        strokeOpacity: "0",
            	        display: "none"
            	    }
            	});
                styleMap = smPolygon;
                if (sFeatureNS != '') {
                    saveObj = new OpenLayers.Strategy.Save();
                    saveObj.events.register('start', null, saveStartPolygon);
                    saveObj.events.register('success', null, saveSuccessPolygon);
                    saveObj.events.register('fail', null, saveFailPolygon);
                }
            	classDraw = "olControlDrawFeaturePolygon";
            }

            var mapWMS = null;
            var mapWFS = null;
            // AppGis | IndexLayer | LayerId | Layer_Name | Visible | StyleMap | Projection | SaveObj | Version | URL | Feature_Type | 
            // Feature_Base | Feature_NS | Geometry_Name | Geometry_Type | DisplayClass_Draw | DisplayClass_Modify | 
            // DisplayClass_Del | Map_Service | Base_Map | Display | IsGroupLayer | Module | Style | SearchAttr
            if(baseMap) mapWMS = new vectorParam(appGis, 0, layerId, layerName, visible, styleMap, projection, 
            		saveObj, version, urlWMS, layerId, sFeatureBase, sFeatureNS, geometryName, geometryType, 
            		classDraw, classModify, classDel, mapService, baseMap, display, isGroupLayer, sModules, sStyleName, bSearchAttr);
            else
        	{    
            	if(sAppGis == '' || appGis == '')
        		{
	            	if(mapService)
	        		{
		            	mapWMS = new vectorParam(appGis, indexLayer++, layerId, layerName, visible, styleMap, projection,
		            		saveObj, version, urlWMS, layerId, sFeatureBase, '', geometryName, geometryType, 
		            		classDraw, classModify, classDel, mapService, baseMap, display, isGroupLayer, sModules, sStyleName, false);
	        		}
	            	if(saveObj != null)
	        		{
		            	mapWFS = new vectorParam(appGis, indexLayer++, layerId, layerName, visible, styleMap, projection,
		            		saveObj, version, urlWFS, layerId, '', sFeatureNS, geometryName, geometryType, 
		            		classDraw, classModify, classDel, false, baseMap, display, isGroupLayer, sModules, sStyleName, bSearchAttr);
	        		} 
        		}
            	else if(appGis != '' && appGis == sAppGis)
            	{
	            	if(mapService)
	        		{
		            	mapWMS = new vectorParam(appGis, indexLayer++, layerId, layerName, visible, styleMap, projection,
		            		saveObj, version, urlWMS, layerId, sFeatureBase, '', geometryName, geometryType, 
		            		classDraw, classModify, classDel, mapService, baseMap, display, isGroupLayer, sModules, sStyleName, false);
	        		}
	            	if(saveObj != null)
	        		{
		            	mapWFS = new vectorParam(appGis, indexLayer++, layerId, layerName, visible, styleMap, projection,
		            		saveObj, version, urlWFS, layerId, '', sFeatureNS, geometryName, geometryType, 
		            		classDraw, classModify, classDel, false, baseMap, display, isGroupLayer, sModules, sStyleName, bSearchAttr);
	        		} 
            	}
        	}
        	if(mapWMS != null) arrayLayersInfo[arrayLayersInfo.length] = mapWMS;
        	if(mapWFS != null) arrayLayersInfo[arrayLayersInfo.length] = mapWFS;
        });
        bReadyLayerXML = true;
    });

    $(data).find('snaps').each(function () {
        var $snapData = $(this);
        if ($snapData.find('Tolerance').text()) tolerance = $snapData.find('Tolerance').text();
        var sNode = $snapData.find('Node').text();
        if (sNode == "1") Node = true;
        var sVertex = $snapData.find('Vertex').text();
        if (sVertex == "1") Vertex = true;
        var sEdge = $snapData.find('Edge').text();
        if (sEdge == "1") Edge = true;
        bReadySnapXML = true;
    });
});

// IndexLayer | Tolerance | Node | Vertex | Edge
var snapArray = new Array();

var vectorObj = new Array(); // chua doi tuong vector (WFS + WMS) trong OpenLayer
var snapObj = new Array();
var drawObj = new Array();
var modifyObj = new Array();
var delObj = new Array();
var delVerticesObj = new Array();
var saveObj = new Array();

/**** delete feature *****/
var isDrawBox = true;
var isConfirm = true;
// mang chua feature sau khi nguoi dung select
var featureDelete = new Array();
// mang chua feature nguoi dung dinh xoa
var featureDeleteAction = new Array();
// mang chua feature nguoi dung da xoa
var featureDeleteFinish = new Array();
var clickMapDelete = false;
// chi so feature hien tai de xoa
var indexFeatureDel = 0;
// mang chua cac hang de hien thong tin feature
var arrTRDelFeature = [];
// mang chua cac hang de hien thong tin feature dinh xoa
var arrTRDelFeatureAction = [];
/*********/
var divMenuEditor = null;
var vectorArray = new Array();

var objLoadXML = null;
var bStart = false;
// doc file XML - file config
function loadXML() {
	if(!bReadySnapXML || !bReadyLanagueXML || !bReadyLayerXML || !bReadyMenuXML || !bReadyAliasXML || arrayLayersInfo.length == 0) {
        objLoadXML = setTimeout('loadXML()', 1000);
    }
    else {
    	// load layers
        // IndexLayer | LayerId | Layer_Name | Visible | StyleMap | Projection | SaveObj | Version | URL | Feature_Type | 
        // Feature_Base | Feature_NS | Geometry_Name | Geometry_Type | DisplayClass_Draw | DisplayClass_Modify | 
        // DisplayClass_Del | Map_Service | Base_Map | Display | IsGroupLayer | Module | Style
    	if (vectorArray == null) vectorArray = new Array();
    	var mapInfo = null;
    	var indexLayer = 0;
    	for(var i=0; i<arrayLayersInfo.length; i++)
		{
    		mapInfo = arrayLayersInfo[i];
    		var sModules = mapInfo.Module;
    		var arrModules = sModules.split(',');
    		var bContain = false;
            for(var j=0; j<arrModules.length; j++)
        	{            	
            	if(parseInt(arrModules[j]) == parseInt(INDEX_MODULE))
        		{
            		bContain = true;
            		break;
        		}
        	}
            if(!bContain) continue;
            if(mapInfo.Base_Map) indexLayer = 0;
            else indexLayer++;
            mapInfo.IndexLayer = indexLayer;
            vectorArray[vectorArray.length] = mapInfo;
            if (mapInfo.Map_Service && !mapInfo.Base_Map) {
                mapWMS[mapWMS.length] = mapInfo;
            }
            else if (!mapInfo.Map_Service && !mapInfo.Base_Map) {
                mapWFS[mapWFS.length] = mapInfo;
            }
		}
    	
    	bReadySnapXML = bReadyLanagueXML = bReadyLayerXML = bReadyMenuXML = true;    	
        clearTimeout(objLoadXML);
        bStart = true;
		initTools();		
		Zoom.initScaleList();
		SnapSetting.loadSnaping();		

		// khoi tao map		
		initMap('map');	

		initLanguage_Search();
		initLanguage_Asset();
		// tai san  
		getFeaturesDistrictCommune();
		/* tree view menu */
		$(".browser-tree").treeview();			
		/* uniform */
		$(".form-mapleft-menu select").uniform();
		jQuery("input[type=checkbox].checkbox-unf").uniform();
		// ban do quy hoach
		if(INDEX_MODULE==ListModule.commonGis){
			isParamBonita = $("#txtActionBonita").val();
			if(isParamBonita=="frombonita"){
				var masohoso = $("#txtMaHoSo").val();
				var layer = $("#txtTableHoSo").val();
				CommonGisBonita.filterFeature(masohoso);
				CommonGisBonita.resetCombo(layer);
				CommonGisBonita.getTypeAction(masohoso,layer);
				ToolMapAction.removeChecked();
				$(".lfr-component").hide();
				$("#banner").hide();
				var param = "type="+ToolMapAction.typeAction+"&masohoso="+masohoso+"&layer="+layer;
				if(ToolMapAction.typeAction=="insert"){
					$.get("/AnGiang-557-portlet/html/commongis/process_coordinate.jsp?"+param,function(data){
						$("#divSelectAssetInsertgeom").html(data.trim());
						$("#divDialog_InsertGeomFromHoSo").dialog("open");
						ToolMapAction.initDraw(layer);	
					});
				}else if(ToolMapAction.typeAction=="update"){
					$.get("/AnGiang-557-portlet/html/commongis/process_coordinate.jsp?"+param,function(data){
						$("#divSelectAssetInsertgeom").html(data.trim());
						$("#divDialog_InsertGeomFromHoSo").dialog("open");
						ToolMapAction.bidding(layer,masohoso);	
					});
				}
			}
		}
		
		
		if(INDEX_MODULE == ListModule.quanlyDQT)
		{			
		}
		else if(INDEX_MODULE == ListModule.quanlyDQT_thematic) // thematic diem quan trac
		{
			var sTypeThematic = $('#txtTypeThematic').val();
			var sFromdate = $('#txtFromdate').val();
			var sTodate = $('#txtTodate').val();
			var sTC = $('#txtMaTC').val();
			var valLopQT = $('#txtLopQT').val();
			var thongtinQT = $('#txtThongtinQT').val();
			var loaiQT_Chitiet = $('#txtLoaiQT').val();
			var arrMadiem = $('#txtItems').val().split(',');
			var sMadiem = '';
			for(var i=0; i<arrMadiem.length; i++)
			{
				if(i==0) sMadiem = "'" + arrMadiem[i] + "'";
				else sMadiem += ",'" + arrMadiem[i] + "'";
			}
			
			var extent = $('#txtExtent').val().split('-');
			var xMin = parseFloat(extent[0]);
			var xMax = parseFloat(extent[1]);
			var yMin = parseFloat(extent[2]);
			var yMax = parseFloat(extent[3]);

			var bounds = new OpenLayers.Bounds(xMin, yMin, xMax, yMax);
			map.zoomToExtent(bounds);
			
			if(sTypeThematic == thematicType.diemquantrac)
			{
				var thamsoQT = $('#txtThamsoQT').val();
				$.ajax(
				{
					url : '/AnGiang-557-portlet/html/giamsatmoitruong/thematic/action/diemquantrac.jsp',
					type: "POST",
					data :
					{
						from: sFromdate,
						to: sTodate,
						tcvn: sTC,
						layerid: valLopQT,
						thongtinqt: thongtinQT,
						loaiqt: loaiQT_Chitiet,
						items: sMadiem,
						thamso: thamsoQT
					},
					success:function(data, textStatus, jqXHR) 
					{
						//data: return data from server
						var sData = data.replace(/^\s+|\s+$/g, "");
						//alert(sData);
						var arrDQT = sData.split('|');
						thematicCurrent.layerAssetName = valLopQT;
						thematicCurrent.printPath = '/AnGiang-557-portlet/html/giamsatmoitruong/thematic/print/diemquantrac.jsp';
						thematicCurrent.diemquantrac(arrDQT);
					},
					error: function(jqXHR, textStatus, errorThrown) 
					{
						alert(LANGUAGE['thematic_msg_thietlaptieuchuan']);
					}
				});
			}
			else if(sTypeThematic == thematicType.chisodiemquantrac)
			{
				var thamsoQT1 = $('#txtThamsoQT1').val();
				var thamsoQT2 = $('#txtThamsoQT2').val();
				var thamsoQT3 = $('#txtThamsoQT3').val();
				$.ajax(
				{
					url : '/AnGiang-557-portlet/html/giamsatmoitruong/thematic/action/chiso_diemquantrac.jsp',
					type: "POST",
					data :
					{
						from: sFromdate,
						to: sTodate,
						layerid: valLopQT,
						thongtinqt: thongtinQT,
						loaiqt: loaiQT_Chitiet,
						items: sMadiem,
						thamso1: thamsoQT1,
						thamso2: thamsoQT2,
						thamso3: thamsoQT3
					},
					success:function(data, textStatus, jqXHR) 
					{
						//data: return data from server
						var sData = data.replace(/^\s+|\s+$/g, "");
						//alert(sData);
						var arrDQT = sData.split('|');
						thematicCurrent.layerAssetName = valLopQT;
						thematicCurrent.printPath = '/AnGiang-557-portlet/html/giamsatmoitruong/thematic/print/chiso_diemquantrac.jsp';
						thematicCurrent.chisodiemquantrac(arrDQT);
					},
					error: function(jqXHR, textStatus, errorThrown) 
					{
						alert(LANGUAGE['thematic_msg_thietlaptieuchuan']);
					}
				});
			}
		}
    }
}
// lop chua cac truong thong tin trong file config
Field_Alias =
{
    Index: 0,
    Name: "",
    Type: "",
    Alias: "",
    IsShow: false,
    /* START NGUYENNV ADD NEW */
    IsSearch: false,
    /* END NGUYENNV ADD NEW */
    IsShowIdentify: false,
    IsEditor: false,
    LayerName: "",
    IsCategory: false,
    TableName: "",
    FieldValue: "",
    FieldDisplay: "",
    IsRequire: false,
    IsPrimaryKey: false,
    IsShowLabel:false,
    controlID: "",
    format: "",
    
    create: function () {
        var len = arguments.length;
        if (len > 0) this.Index = arguments[0];
        if (len > 1) this.Name = arguments[1];
        if (len > 2) this.Type = arguments[2];
        if (len > 3) this.Alias = arguments[3];
        if (len > 4) this.IsShow = arguments[4] == "0" ? false : true;
        /* START NGUYENNV ADD NEW */
        if (len > 5) this.IsSearch = arguments[5] == "0" ? false : true;
        /* END NGUYENNV ADD NEW */
        if (len > 6) this.IsShowIdentify = arguments[6] == "0" ? false : true;
        if (len > 7) this.IsEditor = arguments[7] == "0" ? false : true;
        if (len > 8) this.LayerName = arguments[8];        
        if (len > 9) this.IsCategory = arguments[9] == "1" ? true : false;
        if (len > 10) this.TableName = arguments[10];        
        if (len > 11) this.FieldValue = arguments[11];        
        if (len > 12) this.FieldDisplay = arguments[12];    
        if (len > 13) this.IsRequire = arguments[13] == "1" ? true : false;    
        if (len > 14) this.IsPrimaryKey = arguments[14] == "1" ? true : false;  
        if (len > 15) this.IsShowLabel = arguments[15] == "1" ? true : false;
        if (len > 16) this.controlID = arguments[16];    
        if (len > 17) this.format = arguments[17];
    }
};

var bReadyAliasXML = false;
var arrField_Alias = new Array();
var arrField_Alias_Current = new Array();

$.get(sURLField, function (data) {
    $(data).find('alias').each(function () {
        var $alias = $(this);
        $alias.find('Field').each(function () {
            var $fieldAlias = $(this);
            var sIndex = "0";
            if($fieldAlias.attr('Index') != null) sIndex = $fieldAlias.attr('Index');
            var sName = "";
            if($fieldAlias.attr('Name') != null) sName = $fieldAlias.attr('Name');
            var sType = "";
            if($fieldAlias.attr('Type') != null) sType = $fieldAlias.attr('Type');
            var sAlias = "";
            if($fieldAlias.attr('Alias') != null) sAlias = $fieldAlias.attr('Alias');
            var isShow = "0";
            if($fieldAlias.attr('IsShow') != null) isShow = $fieldAlias.attr('IsShow');
            var isSearch = "0";
            if($fieldAlias.attr('IsSearch') != null) isSearch = $fieldAlias.attr('IsSearch');
            var isShowIdentify = "0";
            if($fieldAlias.attr('IsShowIdentify') != null) isShowIdentify = $fieldAlias.attr('IsShowIdentify');
            var sIsEditor = "0";
            if($fieldAlias.attr('IsEditor') != null) sIsEditor = $fieldAlias.attr('IsEditor');
            var isShowLabel="0";
            if($fieldAlias.attr('IsShowLabel') != null) isShowLabel = $fieldAlias.attr('IsShowLabel');
            var sLayerName = "";
            if($fieldAlias.attr('layerName') != null) sLayerName = $fieldAlias.attr('layerName');
            var isCategory = "0";
            if($fieldAlias.attr('isCategory') != null) isCategory = $fieldAlias.attr('isCategory');
            var sTableName = "";
            if($fieldAlias.attr('TableName') != null) sTableName = $fieldAlias.attr('TableName');
            var sFieldValue = "";
            if($fieldAlias.attr('FieldValue') != null) sFieldValue = $fieldAlias.attr('FieldValue');
            var sFieldDisplay = "";
            if($fieldAlias.attr('FieldDisplay') != null) sFieldDisplay = $fieldAlias.attr('FieldDisplay');
            var isRequire = "0";
            if($fieldAlias.attr('IsRequire') != null) isRequire = $fieldAlias.attr('IsRequire');
            var isPrimaryKey = "";
            if($fieldAlias.attr('PrimaryKey') != null) isPrimaryKey = $fieldAlias.attr('PrimaryKey');
            var sControlID = "";
            if($fieldAlias.attr('controlID') != null) sControlID = $fieldAlias.attr('controlID');
            var sFormatFile = "";
            if($fieldAlias.attr('format') != null) sFormatFile = $fieldAlias.attr('format');
			
            var fieldalias = new Field_Alias.create(sIndex, sName, sType, sAlias, isShow, isSearch, isShowIdentify, sIsEditor, sLayerName, isCategory,
            		sTableName, sFieldValue, sFieldDisplay, isRequire, isPrimaryKey,isShowLabel, sControlID, sFormatFile);
            arrField_Alias[arrField_Alias.length] = fieldalias;

        });
        arrField_Alias.sort
        (
            function (a, b) {
                var x = a.Index - b.Index;
                return x;
            });
        bReadyAliasXML = true;
    });
});
/******* end function input data ************/

/********************************* LANGUAGE **********************************/
function initLanguage_Asset()
{
	// thua dat
	$(base.getElementById("optionNone_TuyenDuong_THUADAT")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_QuanHuyen_THUADAT")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_PhuongXa_THUADAT")).text(LANGUAGE["none"]);
	
	// diem vi pham
	$(base.getElementById("optionNone_QuanHuyen_DIEMVIPHAM")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_DIEMVIPHAM")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_DIEMVIPHAM_TrangthaiVipham")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_DIEMVIPHAM_Loaivipham")).text(LANGUAGE["none"]);
	
	// diem cap phep
	$(base.getElementById("optionNone_LoaiDiemCuoi_LoaiCongTrinh")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_LoaiDiemCuoi_LoaiGiayPhep")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_QuanHuyen_DIEMCAPPHEP")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_DIEMCAPPHEP")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_LoaiDiemCuoi_ChiGioiXayDung")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_LoaiDiemCuoi_SoGiayPhep")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_LoaiDiemCuoi_TrangthaiGiayPhep")).text(LANGUAGE["none"]);	
	// chi gioi xay dung
	$(base.getElementById("optionNone_TuyenDuong_CHIGIOIXAYDUNG")).text(LANGUAGE["none"]);
	
	// chi gioi duong do
	$(base.getElementById("optionNone_TuyenDuong_CHIGIOIDUONGDO")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_QuanHuyen_CHIGIOIDUONGDO")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_PhuongXa_CHIGIOIDUONGDO")).text(LANGUAGE["none"]);
	
	// quy hoach chung
	$(base.getElementById("optionNone_QuanHuyen_QHCHUNG")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_QHCHUNG")).text(LANGUAGE["none"]);
}

function initLanguage_Search()
{
	// tram bien ap
	$(base.getElementById("optionNone_TuyenDuong_TBA_Search")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_TuyenCS_TBA_Search")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_TrangThai_TBA_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_QuanHuyen_TBA_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_TBA_Search")).text(LANGUAGE["none"]);

	// day dan
	$(base.getElementById("optionNone_TuyenCS_DAYDAN_Search")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_TrangThai_DAYDAN_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_KieuDiCap_DAYDAN_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_LoaiCap_DAYDAN_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_QuanHuyen_DAYDAN_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_DAYDAN_Search")).text(LANGUAGE["none"]);	

	// chi gioi xay dung
	$(base.getElementById("optionNone_TuyenDuong_CHIGIOIXAYDUNG_Search")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_QuanHuyen_CHIGIOIXAYDUNG_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_CHIGIOIXAYDUNG_Search")).text(LANGUAGE["none"]);

	// chi gioi duong do
	$(base.getElementById("optionNone_TuyenDuong_CHIGIOIDUONGDO_Search")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_QuanHuyen_CHIGIOIDUONGDO_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_CHIGIOIDUONGDO_Search")).text(LANGUAGE["none"]);

	// tru den
	$(base.getElementById("optionNone_TuyenCS_TRUDEN_Search")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_TrangThai_TRUDEN_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_DVXD_TRUDEN_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_LoaiTD_TRUDEN")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_TuDK_TRUDEN_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_QuanHuyen_TRUDEN_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_TRUDEN_Search")).text(LANGUAGE["none"]);	
	
	// quy hoach chung
	$(base.getElementById("optionNone_QuanHuyen_QHCHUNG_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_QHCHUNG_Search")).text(LANGUAGE["none"]);
	
	// quy hoach chi tiet
	$(base.getElementById("optionNone_QuanHuyen_QHCHITIET_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_QHCHITIET_Search")).text(LANGUAGE["none"]);

	// diem cap phep
	$(base.getElementById("optionNone_LoaiDiemCuoi_GiayPhepViahe")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_LoaiDiemCuoi_GiayPhepThicong")).text(LANGUAGE["none"]);		
	$(base.getElementById("optionNone_LoaiDiemCuoi_LoaiCongTrinh_Search")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_QuanHuyen_DIEMCAPPHEP_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_DIEMCAPPHEP_Search")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_LoaiDiemCuoi_ChiGioiXayDung_Search")).text(LANGUAGE["none"]);

	// diem vi pham
	$(base.getElementById("optionNone_LoaiDiemCuoi_LoaiCongTrinh_Search")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_QuanHuyen_DIEMVIPHAM_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_DIEMVIPHAM_Search")).text(LANGUAGE["none"]);	

	// ho ga
	$(base.getElementById("optionNone_Loaihoga_HOGA_Search")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_Trangthai_HOGA_Search")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_Vatlieu_HOGA_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_Tuyenduong_HOGA_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_Modong_HOGA_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_Hangsx_HOGA_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_QuanHuyen_HOGA_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_HOGA_Search")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_Tuyencong_HOGA_Search")).text(LANGUAGE["none"]);

	// thua dat
	$(base.getElementById("optionNone_TuyenDuong_THUADAT_Search")).text(LANGUAGE["none"]);
	$(base.getElementById("optionNone_QuanHuyen_THUADAT_Search")).text(LANGUAGE["none"]);	
	$(base.getElementById("optionNone_PhuongXa_THUADAT_Search")).text(LANGUAGE["none"]);
}
/*******************************************************************************************/	

/*************************************
			SAVE FEATURE
*************************************/
var bSaveEditting = true;
// POINT
function saveStartPoint(e)
{	
}

function saveSuccessPoint(e)
{
	//alert("Point saved...");
	enableMainPage();
}

function saveFailPoint(e)
{
	alert("Error when saving point...");
	//alert(l.abc);
	
}

// LINE
function saveStartLine()
{
	
}

function saveSuccessLine()
{
	//alert("Line saved...");	
	enableMainPage();
}

function saveFailLine(e)
{
	alert("Error when saving line...");
}

// POLYGON
function saveStartPolygon()
{	
}

function saveSuccessPolygon()
{
	//alert("Polygon saved...");
	enableMainPage();
}

function saveFailPolygon(evt)
{
	//alert(evt);
	alert("Error when saving polygon...");
}
/************************************/
